/**
 * 
 */
package com.youth.fund.hk.hostManager.manager.service.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.youth.fund.hk.hostManager.manager.service.UploadService;

/**
 * @author Sunk_Sky
 *
 *         2014年11月27日下午3:17:50
 */
@Service("UploadService")
public class UploadServiceImpl implements UploadService
{
	// 上传目录
	@Value("${uploaddic}")
	private String path;
	
	@Value("${upload.ip}")
	private String ip;
	
	@Value("${ueditor.suffixPath}")
	private String suffixPath;

	@Override
	public String uploadPDF(MultipartFile file, String name)
	{
		String dirPath = path + "pdf/";

		File dir = new File(dirPath);

		if (!dir.exists())
		{
			dir.mkdirs();
		}

		File targetFile = new File(dirPath + file.getOriginalFilename());

		// 如果有同名文件进行覆盖
		try
		{
			byte[] bytes = file.getBytes();

			FileOutputStream fos = new FileOutputStream(targetFile);

			fos.write(bytes);

			fos.close();

		} catch (FileNotFoundException e)
		{
			e.printStackTrace();

			return "File not fount error";
		} catch (IOException e)
		{
			e.printStackTrace();

			return "IOException error";
		}

		// url请求地址
		String result = this.getURL(file.getOriginalFilename(), "pdf");

		return result;
	}

	private String getHostIP()
	{
		String result = "";
		try
		{
			InetAddress netAddress = InetAddress.getLocalHost();

			result = netAddress.getHostAddress();

		} catch (UnknownHostException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;
	}

	private String getURL(String name, String fileType)
	{
		String result = "";

		//String ip = this.getHostIP();

//		result = result + ip + ":8080/hostManager/static/" + fileType + "/"
//				+ name;
		result = result + ip + "/static/" + fileType + "/"
				+ name;

		return result;
	}

	@Override
	public String uploadImage(MultipartFile file, String name)
	{
		String dirPath = path + "image/";

		File dir = new File(dirPath);

		if (!dir.exists())
		{
			dir.mkdirs();
		}

		File targetFile = new File(dirPath + file.getOriginalFilename());

		// 如果有同名文件进行覆盖
		try
		{
			byte[] bytes = file.getBytes();

			FileOutputStream fos = new FileOutputStream(targetFile);

			fos.write(bytes);

			fos.close();

		} catch (FileNotFoundException e)
		{
			e.printStackTrace();

			return "File not fount error";
		} catch (IOException e)
		{
			e.printStackTrace();

			return "IOException error";
		}

		// url请求地址
		String result = this.getURL(file.getOriginalFilename(), "image");
		
		return result;
	}

	@Override
	public String uploadFlash(MultipartFile file, String name)
	{
		String dirPath = path + "flash/";

		File dir = new File(dirPath);

		if (!dir.exists())
		{
			dir.mkdirs();
		}

		File targetFile = new File(dirPath + file.getOriginalFilename());

		// 如果有同名文件进行覆盖
		try
		{
			byte[] bytes = file.getBytes();

			FileOutputStream fos = new FileOutputStream(targetFile);

			fos.write(bytes);

			fos.close();

		} catch (FileNotFoundException e)
		{
			e.printStackTrace();

			return "File not fount error";
		} catch (IOException e)
		{
			e.printStackTrace();

			return "IOException error";
		}

		// url请求地址
		String result = this.getURL(file.getOriginalFilename(), "flash");
		
		return result;
	}

	@Override
	public String uoloadUeditorImage(MultipartFile file, String ueditorpath)
	{
		
		String dirPath = ueditorpath + this.suffixPath;

		File dir = new File(dirPath);

		if (!dir.exists())
		{
			dir.mkdirs();
		}

		File targetFile = new File(dirPath + file.getOriginalFilename());

		// 如果有同名文件进行覆盖
		try
		{
			byte[] bytes = file.getBytes();

			FileOutputStream fos = new FileOutputStream(targetFile);

			fos.write(bytes);

			fos.close();

		} catch (FileNotFoundException e)
		{
			e.printStackTrace();

			return "File not fount error";
		} catch (IOException e)
		{
			e.printStackTrace();

			return "IOException error";
		}

		// url请求地址
		String result = this.getURL(file.getOriginalFilename(), "image");
		
		return result;
	}

}
